﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using OestsCommon.Model;
using Utils;

namespace OestsDataBase
{
    public class TrueOrFalseDAL
    {
        /// <summary>
        /// 返回id
        /// </summary>
        /// <param name="tf"></param>
        /// <returns></returns>
        public static int Add(TrueOrFalse tf)
        {

            int nCorrect = tf.Correct ? 1 : 0;
            string strSql = string.Format("insert into T_TrueOrFalse (majorId, postId, title, correct, difficultylevel, projectId,defaultscore) " +
                "values({0},{1},'{2}',{3},{4},{5},{6});SELECT @@Identity",
                tf.MajorId, tf.PostId, tf.Title, nCorrect, (int)tf.Level, tf.ProjectId,tf.DefaultScore);
            object obj = SqlHelper.ExecuteScalar(strSql);//返回dbid
            return Int32.Parse(obj.ToString());
        }

        private static TrueOrFalse ReadData(DataRow dr)
        {
            TrueOrFalse tf = new TrueOrFalse();
            tf.Id = Int32.Parse(dr["id"].ToString());
            //tf.majorId = int.Parse(dr["majorId"].ToString());

            tf.MajorId = dr["majorId"].Equals(DBNull.Value) ? 0 : int.Parse(dr["majorId"].ToString());
            tf.PostId = dr["postId"].Equals(DBNull.Value) ? 0 : int.Parse(dr["postId"].ToString());
            tf.Level = (DifficultyLevel)(dr["difficultylevel"].Equals(DBNull.Value) ? 0 : Int32.Parse(dr["difficultylevel"].ToString()));

            tf.Title = dr["title"].ToString();
            tf.Correct = (bool)dr["correct"];
            tf.DefaultScore = dr["defaultscore"].Equals(DBNull.Value) ? 0 : int.Parse(dr["defaultscore"].ToString());
            tf.ProjectId = dr["projectId"].Equals(DBNull.Value) ? 0 : int.Parse(dr["projectId"].ToString());
            return tf;
        }

        public static List<TrueOrFalse> GetData()
        {
            List<TrueOrFalse> list = new List<TrueOrFalse>();
            string strSql = "select * from T_TrueOrFalse ";
            DataTable dt = SqlHelper.ExecuteDataTable(strSql);
            if (dt == null) { return null; }
            foreach (DataRow dr in dt.Rows)
            {
                TrueOrFalse tf = ReadData(dr);
                list.Add(tf);
            }
            return list;
        }

        public static bool Delete(int id)
        {
            string strSql = string.Format("delete from T_TrueOrFalse where id = @id");
            int n = SqlHelper.ExecuteNonQuery(strSql, new SqlParameter("@id", id));
            if (n != 0)
            {
                return true;
            }
            return false;
        }

        public static TrueOrFalse GetTrueOrFalseById(int id)
        {
            string strSql = "select * from T_TrueOrFalse where id = @id ";
            DataTable dt = SqlHelper.ExecuteDataTable(strSql, new SqlParameter("@id", id));
            if (dt == null || dt.Rows.Count == 0) { return null; }
            DataRow dr = dt.Rows[0];
            TrueOrFalse tf = ReadData(dr);
            return tf;
        }

        public static bool Modify(TrueOrFalse tf)
        {
            int nCorrect = tf.Correct ? 1 : 0;

            string strSql = string.Format("update T_TrueOrFalse set majorId='{0}',postId={1},title='{2}',correct={3},difficultylevel={4},projectId={5} where id = @id",
                tf.MajorId, tf.PostId, tf.Title, nCorrect, tf.Level, tf.ProjectId);
            int n = SqlHelper.ExecuteNonQuery(strSql, new SqlParameter("@id", tf.Id));
            if (n != 0)
            {
                return true;
            }
            return false;
        }

    }
}
